<?php
/**
 * Imports
 */ 
require_once("constants/ConstantsApp.php");
require_once("external/adodb5/adodb.inc.php");
require_once("external/pear/Mail.php");


?><?php
class GlobalApp
{
	static $globalAppInstance;
	
	/**
	* @var ADOdbConnection
	*/	 
	static public $objConnection;
	
	/**
	* @var string
	*/	
	static private $strDriver;
	
	/**
	* @var string
	*/		
	static private $strHost;
	
	/**
	* @var string
	*/		
	static private $strDataBase;
	
	/**
	* @var string
	*/			
	static private $strUserName;
	
	/**
	* @var string
	*/		
	static private $strPassword;
		

    private function __construct() {

    }
 
    protected function __clone() {

    }	
	
    static public function getInstance() 
    {
        if(is_null(self::$globalAppInstance))
        {
            self::$globalAppInstance = new self();

        }
        
        return self::$globalAppInstance;
    }

   	
	final public function isLocalHost()
	{
		
		$blnReturn = false;
		if ( $_SERVER["SERVER_ADDR"] == "127.0.0.1" )
		{
			$blnReturn = true;
		}
		
		return $blnReturn;
	}   	
   	

	final private function defineDataBaseSettings()
	{
		if ( self::isLocalHost() )
		{
			self::$strDriver	= ConstantsApp::getProperty("ADODB_LOCAL_DRIVER");
			self::$strHost		= ConstantsApp::getProperty("ADODB_LOCAL_HOST");
			self::$strUserName  = ConstantsApp::getProperty("ADODB_LOCAL_USERNAME");
			self::$strPassword  = ConstantsApp::getProperty("ADODB_LOCAL_PASSWORD");
			self::$strDataBase	= ConstantsApp::getProperty("ADODB_LOCAL_DATABASE");
		}
		else
		{
			self::$strDriver	= ConstantsApp::getProperty("ADODB_PROD_DRIVER");
			self::$strHost		= ConstantsApp::getProperty("ADODB_PROD_HOST");
			self::$strUserName  = ConstantsApp::getProperty("ADODB_PROD_USERNAME");
			self::$strPassword  = ConstantsApp::getProperty("ADODB_PROD_PASSWORD");
			self::$strDataBase	= ConstantsApp::getProperty("ADODB_PROD_DATABASE");
		}
	}   	
	
	static public function initializeComponent()
	{
			self::defineDataBaseSettings();
		

			self::$objConnection = NewADOConnection('mysql');
			self::$objConnection->setFetchMode(ADODB_FETCH_ASSOC);		
			
			try 
			{
				$blnConnect = self::$objConnection->Connect(self::$strHost, self::$strUserName, self::$strPassword, self::$strDataBase);
				
			}
			catch (Exception $ex) 
			{
			 	throw new Exception($ex);
			}
	}	
   	
    final public function &executeSQL( $strSQL, $params = null )
    {
    	if (is_null(self::$objConnection))
    	{  
    		GlobalApp::initializeComponent();
    	}
    	
    	if ( is_null($params) )
    	{
    		$objRs = self::$objConnection->Execute($strSQL);	
    	}
    	else
    	{
			$objRs = self::$objConnection->Execute($strSQL, $params);    		
    	}
    	
 		if ( self::$objConnection->ErrorNo() != 0 )
 		{
 			throw new Exception( self::$objConnection->ErrorMsg(), self::$objConnection->ErrorNo() );
 		}    	

    	return $objRs; 	
    }
   	
	public function __sleep() 
	{ 
		$this->objConnection->close();
		return array_keys(get_object_vars($this)); 
	}	   	
   	
	final public function setADOdbFetchMode( $strConstValue )
	{
		$this->objConnection->SetFetchMode( $strConstValue ); 
	}   	
   	
    final public function getConnection()
    {
    	return $this->objConnection;
    }

    
    final public function setDriver( $strValue )
    {
    	$this->strDriver = $strValue;
    }
    
   
    final public function setHost( $strValue )
    {
    	$this->strHost = $strValue;
    }    
    
  
    final public function setUserName( $strValue )
    {
    	$this->strUserName = $strValue;
    }
    
    
    final public function setPassword( $strValue )
    {
    	$this->strPassword = $strValue;
    }    
    
  
    final public function setDataBase( $strValue )
    {
    	$this->strDataBase = $strValue;
    }
    
    final public function sendMail( $to, $headers, $body )
    {
 		$smtp = Mail::factory('smtp',
		array (
				'host' => ConstantsApp::getProperty("SMTP_HOST"),    
				'auth' =>  true,    
				'username' =>  ConstantsApp::getProperty("SMTP_USERNAME"),
    			'password' =>  ConstantsApp::getProperty("SMTP_PASSWORD"),
				'port' =>  ConstantsApp::getProperty("SMTP_PORT")
				)
		);
		
		$mail = $smtp->send($to, $headers, $body);
	
    }    
	
}
?>